﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Foundation.WFDB;
using BPM.Base.Tools;

namespace BPM.Foundation.WFService
{
    public partial interface IWorkflowService
    {
        [System.ServiceModel.OperationContract]
        List<WF_InstanceState_V> getTaskNeedTransact(string pid, string userNo);

        [System.ServiceModel.OperationContract]
        List<WF_InstanceState_V> getTaskCanIncept(string pid, string departmentNo, string dutyNo);

        [System.ServiceModel.OperationContract]
        List<WF_InstanceState_V> getTaskCanAssign(string pid, string departmentNo, string dutyNo, string userNo);

        [System.ServiceModel.OperationContract]
        List<WF_InstanceState_V> getTaskCanSee(string pid, string departmentNo, string dutyNo, string userNo);

        [System.ServiceModel.OperationContract]
        List<WF_InstanceState_V> getTaskCanManage(string pid, string departmentNo, string dutyNo, string userNo);

        [System.ServiceModel.OperationContract]
        List<WF_InstanceState_V> getTaskTimelimit(string pid, string departmentNo, string dutyNo, string userNo, int timelimit, string type);

        [System.ServiceModel.OperationContract]
        List<WF_Instance_V> getProcessCanSee(string pid, string departmentNo, string dutyNo, string userNo);

        [System.ServiceModel.OperationContract]
        List<WF_Instance_V> getProcessCanManage(string pid, string departmentNo, string dutyNo, string userNo);

        [System.ServiceModel.OperationContract]
        List<WF_Instance_V> getProcessTimelimit(string pid, string departmentNo, string dutyNo, string userNo, int timelimit);

        [System.ServiceModel.OperationContract]
        List<WF_Instance_V> getProcessCreate(string pid, string departmentNo, string dutyNo, string userNo);

        [System.ServiceModel.OperationContract]
        List<WF_Instance_V> getProcessParticipate(string pid, string departmentNo, string dutyNo, string userNo);

        [System.ServiceModel.OperationContract]
        List<WF_Instance_V> searchProcess(string pid, string departmentNo, string dutyNo, string userNo,string type);

        [System.ServiceModel.OperationContract]
        List<WF_InstanceState_V> searchTask(string pid, string departmentNo, string dutyNo, string userNo, string type);

        [System.ServiceModel.OperationContract]
        List<string> getSpecialStatePower(string pid, Guid instanceID, Guid stateInstanceID, string type);

        [System.ServiceModel.OperationContract]
        List<string> getStartSpecialPowerFromTemplate(string pid, string templateID, string templateEdition);
    
        [System.ServiceModel.OperationContract]
        List<WF_InstanceState_V> getWFInstanceState(string pid, string instanceId);

        [System.ServiceModel.OperationContract]
        List<WF_Instance_V> getWFInstanceByCondition(string pid, string templateId, string department, string startUser, string DatarowId,string status);
   

    }

    public partial class WorkflowService : IWorkflowService
    {
        //[8.1	得到需要办理的任务]
        public List<WF_InstanceState_V> getTaskNeedTransact(string pid, string userNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (userNo == null)
                {
                    return null;
                }

                //[2]
                if (userNo == "")
                {
                    var instanceState = wfDB.WF_InstanceState_V.Where(p => p.runState == "wait");
                    return instanceState.ToList();
                }

                //[3]
                if (userNo != "")
                {
                    List<WF_InstanceState_V> _instanceStateList = new List<WF_InstanceState_V>();
                    var instanceState = wfDB.WF_InstanceState_V.Where(p => p.runState == "wait" && p.transactUser.Contains(userNo));
                    foreach (var i in instanceState)
                    {
                        Boolean isSucceed = check(i.transactUser, userNo);
                        if (isSucceed == true)
                        {
                            _instanceStateList.Add(i);
                        }
                    }
                    return _instanceStateList;
                }

                return null;
            }

        }

        //[内部调用]
        private Boolean check(String str1, String str2)
        {

            if (String.IsNullOrEmpty(str1) || String.IsNullOrEmpty(str2))
            {
                return false;
            }
            String[] _tempStr = str1.Split(',');
            foreach (String s in _tempStr)
            {
                if (s == str2)
                {
                    return true;
                }
            }
            return false;
        }

        //[8.2	得到可以接件的任务]
        public List<WF_InstanceState_V> getTaskCanIncept(string pid, string departmentNo, string dutyNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (departmentNo == null || dutyNo == null)
                {
                    return null;
                }

                //[2]
                //var instanceState = wfDB.WF_InstanceState_V.Where(p=>p.transactUser == "" && p.runState == "wait");
                //[2] 
                var instanceState = wfDB.WF_InstanceState_V.Where(p =>
                    ((p.transactUser == "" && p.runState == "wait") && (p.assignDepartment == "" && p.assignDuty == ""))
                    ||
                    ((p.transactUser == "" && p.runState == "wait") && (p.transactType == "组接件组分配" || p.transactType == "动态组接件组分配")));
                if (instanceState == null)
                {
                    return null;
                }

                //[a]
                if (departmentNo == "" && dutyNo == "")
                {
                    return instanceState.ToList();
                }

                //[b]
                if (departmentNo != "" && dutyNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.inceptDepartment == departmentNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //
                List<WF_InstanceState_V> _instanceStateList = new List<WF_InstanceState_V>();

                //[c]
                if (departmentNo == "" && dutyNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.inceptDuty.Contains(dutyNo));
                    foreach (var i in instanceStateTemp)
                    {
                        Boolean isSucceed = check(i.inceptDuty, dutyNo);
                        if (isSucceed == true)
                        {
                            _instanceStateList.Add(i);
                        }
                    }
                    return _instanceStateList;
                }

                //[d]
                if (departmentNo != "" && dutyNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.inceptDepartment == departmentNo && p.inceptDuty.Contains(dutyNo));
                    foreach (var i in instanceStateTemp)
                    {
                        Boolean isSucceed = check(i.inceptDuty, dutyNo);
                        if (isSucceed == true)
                        {
                            _instanceStateList.Add(i);
                        }
                    }
                    return _instanceStateList;
                }

                return null;
            }
        }

        //[8.3	得到可以分配的任务]
        public List<WF_InstanceState_V> getTaskCanAssign(string pid, string departmentNo, string dutyNo, string userNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (departmentNo == null || dutyNo == null || userNo == null)
                {
                    return null;
                }

                //[2]
                var instanceState = wfDB.WF_InstanceState_V.Where(p => (p.transactUser == "" && p.runState == "wait"));

                if (instanceState == null)
                {
                    return null;
                }

                //[a]
                if (departmentNo == "" && dutyNo == "" && userNo == "")
                {
                    return instanceState.ToList();
                }

                //[b]
                if (departmentNo != "" && dutyNo == "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.assignDepartment == departmentNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[c]
                if (departmentNo == "" && dutyNo != "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.assignDuty == dutyNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[d]
                if (departmentNo != "" && dutyNo != "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.assignDepartment == departmentNo && p.assignDuty == dutyNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[e]
                if (departmentNo == "" && dutyNo == "" && userNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.assignUser == userNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[f]
                if (departmentNo != "" && dutyNo != "" && userNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.assignDepartment == departmentNo && p.assignDuty == dutyNo && p.assignUser == userNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //var instanceStateTemp = instanceState.Where(p => p.assignDepartment == "" && p.assignDuty == "");

                return null;
            }
        }

        //[8.4	得到可以查看的任务]
        public List<WF_InstanceState_V> getTaskCanSee(string pid, string departmentNo, string dutyNo, string userNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (departmentNo == null || dutyNo == null || userNo == null)
                {
                    return null;
                }

                //[2]
                var instanceState = wfDB.WF_InstanceState_V.Where(p => p.runState == "wait");
                if (instanceState == null)
                {
                    return null;
                }

                //[a]
                if (departmentNo == "" && dutyNo == "" && userNo == "")
                {
                    return instanceState.ToList();
                }

                //[b]
                if (departmentNo != "" && dutyNo == "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.seeDepartment == departmentNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[c]
                if (departmentNo == "" && dutyNo != "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.seeDuty == dutyNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[d]
                if (departmentNo != "" && dutyNo != "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.seeDepartment == departmentNo && p.seeDuty == dutyNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[e]
                if (departmentNo == "" && dutyNo == "" && userNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.seeUser == userNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[f]
                if (departmentNo != "" && dutyNo != "" && userNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.seeDepartment == departmentNo && p.seeDuty == dutyNo && p.seeUser == userNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                return null;
            }
        }

        //[8.5	得到可以管理的任务]
        public List<WF_InstanceState_V> getTaskCanManage(string pid, string departmentNo, string dutyNo, string userNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (departmentNo == null || dutyNo == null || userNo == null)
                {
                    return null;
                }

                //[2]
                var instanceState = wfDB.WF_InstanceState_V.Where(p => p.runState == "wait");
                if (instanceState == null)
                {
                    return null;
                }

                //[a]
                if (departmentNo == "" && dutyNo == "" && userNo == "")
                {
                    return instanceState.ToList();
                }

                //[b]
                if (departmentNo != "" && dutyNo == "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.manageDepartment == departmentNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[c]
                if (departmentNo == "" && dutyNo != "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.manageDuty == dutyNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[d]
                if (departmentNo != "" && dutyNo != "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.manageDepartment == departmentNo && p.manageDuty == dutyNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }
                //[e]
                if (departmentNo == "" && dutyNo == "" && userNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.manageUser == userNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }
                //[f]
                if (departmentNo != "" && dutyNo != "" && userNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.manageDepartment == departmentNo && p.manageDuty == dutyNo && p.manageUser == userNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                return null;
            }
        }

        //[8.6	得到超时任务]
        public List<WF_InstanceState_V> getTaskTimelimit(string pid, string departmentNo, string dutyNo, string userNo, int timelimit, string type)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (departmentNo == null || dutyNo == null || userNo == null)
                {
                    return null;
                }

                //[2]
                var instanceState = wfDB.WF_InstanceState_V.Where(p => p.runState == "wait");
                if (instanceState == null)
                {
                    return null;
                }

                if (type == "全部")
                {
                    instanceState = instanceState.Where(p => p.transactTimelimit <= timelimit || p.inceptTimelimit <= timelimit || p.assignTimelimit <= timelimit);
                }
                else if (type == "分配")
                {
                    instanceState = instanceState.Where(p => p.transactTimelimit <= timelimit);
                }
                else if (type == "办理")
                {
                    instanceState = instanceState.Where(p => p.inceptTimelimit <= timelimit);
                }
                else if (type == "接件")
                {
                    instanceState = instanceState.Where(p => p.assignTimelimit <= timelimit);
                }

                //[a]
                if (departmentNo == "" && dutyNo == "" && userNo == "")
                {
                    return instanceState.ToList();
                }

                //[b]
                if (departmentNo != "" && dutyNo == "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.assignDepartment == departmentNo || p.inceptDepartment == departmentNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[c]
                if (departmentNo == "" && dutyNo != "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.assignDuty == dutyNo || p.inceptDuty == dutyNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                //[d]
                if (departmentNo != "" && dutyNo != "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => (p.inceptDepartment == departmentNo && p.inceptDuty == dutyNo) || (p.assignDepartment == departmentNo && p.assignDuty == dutyNo));
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }
                //[e]
                if (departmentNo == "" && dutyNo == "" && userNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.assignUser == userNo || p.transactUser == userNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }
                //[f]
                if (departmentNo != "" && dutyNo != "" && userNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.assignDepartment == departmentNo && p.assignDuty == dutyNo && p.assignUser == userNo);
                    if (instanceStateTemp == null)
                    {
                        return null;
                    }
                    return instanceStateTemp.ToList();
                }

                return null;
            }

        }

        //[8.7	得到可以查看的流程]
        public List<WF_Instance_V> getProcessCanSee(string pid, string departmentNo, string dutyNo, string userNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (departmentNo == null || dutyNo == null || userNo == null)
                {
                    return null;
                }

                //[2]
                var instance = wfDB.WF_Instance_V.Where(p => p.runState == "wait");
                if (instance == null)
                {
                    return null;
                }
                //[a]
                if (departmentNo == "" && dutyNo == "" && userNo == "")
                {
                    return instance.ToList();
                }

                //[b]
                if (departmentNo != "" && dutyNo == "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.seeDepartment == departmentNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                //[c]
                if (departmentNo == "" && dutyNo != "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.seeDuty == dutyNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                //[d]
                if (departmentNo == "" && dutyNo == "" && userNo != "")
                {
                    var instanceTemp = instance.Where(p => p.seeUser == userNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                //[e]
                if (departmentNo != "" && dutyNo != "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.seeDepartment == departmentNo && p.seeDuty == dutyNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                //[f]
                if (departmentNo != "" && dutyNo != "" && userNo != "")
                {
                    var instanceTemp = instance.Where(p => p.seeDepartment == departmentNo && p.seeDuty == dutyNo && p.seeUser == userNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                return null;
            }
        }

        //[8.8	得到可以管理的流程]
        public List<WF_Instance_V> getProcessCanManage(string pid, string departmentNo, string dutyNo, string userNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (departmentNo == null || dutyNo == null || userNo == null)
                {
                    return null;
                }

                //[2]
                var instance = wfDB.WF_Instance_V.Where(p => p.runState == "wait");
                if (instance == null)
                {
                    return null;
                }
                //[a]
                if (departmentNo == "" && dutyNo == "" && userNo == "")
                {
                    return instance.ToList();
                }

                //[b]
                if (departmentNo != "" && dutyNo == "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.manageDepartment == departmentNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }


                //[c]
                if (departmentNo == "" && dutyNo != "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.manageDuty == dutyNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                //[d]
                if (departmentNo == "" && dutyNo == "" && userNo != "")
                {
                    var instanceTemp = instance.Where(p => p.manageUser == userNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                //[e]
                if (departmentNo != "" && dutyNo != "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.manageDepartment == departmentNo && p.manageDuty == dutyNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }


                //[f]
                if (departmentNo != "" && dutyNo != "" && userNo != "")
                {
                    var instanceTemp = instance.Where(p => p.manageDepartment == departmentNo && p.manageDuty == dutyNo && p.manageUser == userNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                return null;
            }
        }

        //[8.9	得到超时流程]
        public List<WF_Instance_V> getProcessTimelimit(string pid, string departmentNo, string dutyNo, string userNo, int timelimit)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (departmentNo == null || dutyNo == null || userNo == null)
                {
                    return null;
                }

                //[2]
                var instance = wfDB.WF_Instance_V.Where(p => p.runState == "wait" && p.residualTimelimit <= timelimit);
                if (instance == null)
                {
                    return null;
                }
                //[a]
                if (departmentNo == "" && dutyNo == "" && userNo == "")
                {
                    return instance.ToList();
                }

                //[b]
                if (departmentNo != "" && dutyNo == "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.startDepartment == departmentNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }


                //[c]
                if (departmentNo == "" && dutyNo != "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.startDuty == dutyNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                //[d]
                if (departmentNo == "" && dutyNo == "" && userNo != "")
                {
                    var instanceTemp = instance.Where(p => p.startUser == userNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                //[e]
                if (departmentNo != "" && dutyNo != "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.startDepartment == departmentNo && p.startDuty == dutyNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }


                //[f]
                if (departmentNo != "" && dutyNo != "" && userNo != "")
                {
                    var instanceTemp = instance.Where(p => p.startDepartment == departmentNo && p.startDuty == dutyNo && p.startUser == userNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                return null;
            }
        }

        //[8.10	得到启动未完成的流程]
        public List<WF_Instance_V> getProcessCreate(string pid, string departmentNo, string dutyNo, string userNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (departmentNo == null || dutyNo == null || userNo == null)
                {
                    return null;
                }

                //[2]
                var instance = wfDB.WF_Instance_V.Where(p => p.runState == "wait");
                if (instance == null)
                {
                    return null;
                }
                //[a]
                if (departmentNo == "" && dutyNo == "" && userNo == "")
                {
                    return instance.ToList();
                }

                //[b]
                if (departmentNo != "" && dutyNo == "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.startDepartment == departmentNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                //[c]
                if (departmentNo == "" && dutyNo != "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.startDuty == dutyNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                //[d]
                if (departmentNo == "" && dutyNo == "" && userNo != "")
                {
                    var instanceTemp = instance.Where(p => p.startUser == userNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                //[e]
                if (departmentNo != "" && dutyNo != "" && userNo == "")
                {
                    var instanceTemp = instance.Where(p => p.startDepartment == departmentNo && p.startDuty == dutyNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }


                //[f]
                if (departmentNo != "" && dutyNo != "" && userNo != "")
                {
                    var instanceTemp = instance.Where(p => p.startDepartment == departmentNo && p.startDuty == dutyNo && p.startUser == userNo);
                    if (instanceTemp == null)
                    {
                        return null;
                    }
                    return instanceTemp.ToList();
                }

                return null;
            }
        }

        //[8.11	得到参与未完成的流程]
        public List<WF_Instance_V> getProcessParticipate(string pid, string departmentNo, string dutyNo, string userNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (departmentNo == null || dutyNo == null || userNo == null)
                {
                    return null;
                }

                //[2]
                var instanceState = wfDB.WF_InstanceState_V.Where(p => p.runState == "wait");
                if (instanceState == null)
                {
                    return null;
                }

                List<WF_Instance_V> _instanceList = new List<WF_Instance_V>();

                //[a]
                if (departmentNo == "" && dutyNo == "" && userNo == "")
                {
                    foreach (var i in instanceState)
                    {
                        _instanceList.Add(wfDB.WF_Instance_V.Single(p => p.instanceID == i.instanceID));
                    }

                    return _instanceList;
                }

                //[b]
                if (departmentNo != "" && dutyNo == "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.inceptDepartment == departmentNo);

                    if (instanceStateTemp == null)
                    {
                        return null;
                    }

                    foreach (var i in instanceStateTemp)
                    {
                        _instanceList.Add(wfDB.WF_Instance_V.Single(p => p.instanceID == i.instanceID));
                    }

                    return _instanceList;
                }

                //[c]
                if (departmentNo == "" && dutyNo != "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.inceptDuty == dutyNo);

                    if (instanceStateTemp == null)
                    {
                        return null;
                    }

                    foreach (var i in instanceStateTemp)
                    {
                        _instanceList.Add(wfDB.WF_Instance_V.Single(p => p.instanceID == i.instanceID));
                    }

                    return _instanceList;
                }

                //[d]
                if (departmentNo == "" && dutyNo == "" && userNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.transactUser == userNo);

                    if (instanceStateTemp == null)
                    {
                        return null;
                    }

                    foreach (var i in instanceStateTemp)
                    {
                        _instanceList.Add(wfDB.WF_Instance_V.Single(p => p.instanceID == i.instanceID));
                    }

                    return _instanceList;
                }

                //[e]
                if (departmentNo != "" && dutyNo != "" && userNo == "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.inceptDepartment == departmentNo && p.inceptDuty == dutyNo);

                    if (instanceStateTemp == null)
                    {
                        return null;
                    }

                    foreach (var i in instanceStateTemp)
                    {
                        _instanceList.Add(wfDB.WF_Instance_V.Single(p => p.instanceID == i.instanceID));
                    }

                    return _instanceList;
                }

                //[f]
                if (departmentNo != "" && dutyNo != "" && userNo != "")
                {
                    var instanceStateTemp = instanceState.Where(p => p.inceptDepartment == departmentNo && p.inceptDuty == dutyNo && p.transactUser == userNo);

                    if (instanceStateTemp == null)
                    {
                        return null;
                    }

                    foreach (var i in instanceStateTemp)
                    {
                        _instanceList.Add(wfDB.WF_Instance_V.Single(p => p.instanceID == i.instanceID));
                    }

                    return _instanceList;

                }
                return null;
            }

        }

        //[8.12	流程查询]
        public List<WF_Instance_V> searchProcess(string pid, string departmentNo, string dutyNo, string userNo, string type)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                return wfDB.WF_Instance_V.ToList();
            }
        }

        //[8.13	查询任务]
        public List<WF_InstanceState_V> searchTask(string pid, string departmentNo, string dutyNo, string userNo, string type)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                var v = wfDB.WF_InstanceState_V.Where(p => p.runState == "wait");
                return v.ToList();
            }
        }

        //[8.14	得到状态特殊权限列表]
        public List<string> getSpecialStatePower(string pid, Guid instanceID, Guid stateInstanceID, string type)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                List<string> powerListPossible;              //可能的权限列表
                List<string> powerListFromInstanceState;     //instanceState中配置的可用权限
                List<string> powerListForbidden;             //被禁止的权限
                List<string> powerListResult;                //最终的权限
                WF_Instance_V wf_Instance = wfDB.WF_Instance_V.SingleOrDefault(p => p.instanceID == instanceID);
                WF_InstanceState_V wf_InstanceState =
                wfDB.WF_InstanceState_V.SingleOrDefault(p => p.instanceID == instanceID && p.stateInstanceID == stateInstanceID);
                powerListForbidden = new List<string>();
                if (wf_Instance.isFreeze == 1) powerListForbidden.Add("冻结");
                if (wf_Instance.isFreeze == 0) powerListForbidden.Add("解冻");
                if (wf_InstanceState.transactType == "到人" || wf_InstanceState.transactType == "动态到人")
                {
                    powerListForbidden.Add("退回到组"); powerListForbidden.Add("拒绝分配");
                }
                if (wf_InstanceState.transactType == "组分配" || wf_InstanceState.transactType == "动态组分配")
                {
                    powerListForbidden.Add("退回到组");
                }
                if ((wf_InstanceState.transactType == "组分配" || wf_InstanceState.transactType == "动态组分配") && wf_InstanceState.transactUser == "")
                {
                    powerListForbidden.Add("拒绝分配");
                }
                if (wf_InstanceState.transactType == "组接件" || wf_InstanceState.transactType == "动态组接件")
                {
                    powerListForbidden.Add("拒绝分配");
                }
                if ((wf_InstanceState.transactType == "组接件" || wf_InstanceState.transactType == "动态组接件") && wf_InstanceState.transactUser == "")
                {
                    powerListForbidden.Add("退回到组");
                }
                if ((wf_InstanceState.transactType == "组接件组分配" || wf_InstanceState.transactType == "动态组接件组分配") && wf_InstanceState.transactUser == "")
                {
                    powerListForbidden.Add("退回到组"); powerListForbidden.Add("拒绝分配");
                }
                if (wf_InstanceState.transactType == "子流程回归")
                {
                    powerListForbidden.Add("延期"); powerListForbidden.Add("申请延期"); powerListForbidden.Add("退回到组"); powerListForbidden.Add("拒绝分配");
                }
                if (wf_InstanceState.transactType == "系统处理")
                {
                    powerListForbidden.Add("延期"); powerListForbidden.Add("申请延期"); powerListForbidden.Add("退回到组"); powerListForbidden.Add("拒绝分配");
                }
                if (wf_InstanceState.transactUser == "")
                {
                    powerListForbidden.Add("取消办理人"); powerListForbidden.Add("催办");
                }

                switch (type)
                {
                    case "办理人":
                        powerListPossible = new List<string>() { "延期", "申请延期", "结束流程", "冻结", "解冻", "退回到组", "拒绝分配", "授权他人", "指定下一办理人", "必须指定下一办理人", "指定下一办理权限" };
                        powerListFromInstanceState = wf_InstanceState.transactPower.Split(',').ToList();
                        powerListResult = powerListPossible.Intersect(powerListFromInstanceState).ToList();
                        powerListResult = powerListResult.Except(powerListForbidden).ToList();
                        if (powerListResult.Exists(delegate(string power) { return power == "指定下一办理人"; }) && powerListResult.Exists(delegate(string power) { return power == "必须指定下一办理人"; })
                            || powerListResult.Exists(delegate(string power) { return power == "指定下一办理人"; }) && powerListResult.Exists(delegate(string power) { return power == "指定下一办理权限"; })
                            || powerListResult.Exists(delegate(string power) { return power == "必须指定下一办理人"; }) && powerListResult.Exists(delegate(string power) { return power == "指定下一办理权限"; }))
                            powerListResult = powerListResult.Except(new List<string>() { "指定下一办理人", "必须指定下一办理人", "指定下一办理权限" }).ToList();
                        return powerListResult;
                    case "接件人":
                        powerListPossible = new List<string>();
                        powerListResult = powerListPossible;
                        powerListResult = powerListResult.Except(powerListForbidden).ToList();
                        return powerListResult;
                    case "分配人":
                        powerListPossible = new List<string>() { "延期", "申请延期", "结束流程", "冻结", "解冻" };
                        powerListFromInstanceState = wf_InstanceState.assignPower.Split(',').ToList();
                        powerListResult = powerListPossible.Intersect(powerListFromInstanceState).ToList();
                        powerListResult = powerListResult.Except(powerListForbidden).ToList();
                        return powerListResult;
                    case "管理人":
                        powerListPossible = new List<string>() { "延期", "结束流程", "冻结", "解冻", "退回到组", "拒绝分配", "授权他人", "取消办理人", "催办" };
                        powerListResult = powerListPossible;
                        powerListResult = powerListResult.Except(powerListForbidden).ToList();
                        return powerListResult;
                    case "查看人":
                        powerListPossible = new List<string>() { "催办" };
                        powerListFromInstanceState = wf_InstanceState.seePower.Split(',').ToList();
                        powerListResult = powerListPossible.Intersect(powerListFromInstanceState).ToList();
                        powerListResult = powerListResult.Except(powerListForbidden).ToList();
                        return powerListResult;
                    default:
                        return new List<string>();
                }
            }
        }

        //[8.15	得到模板的启动特殊权限列表]
        public List<string> getStartSpecialPowerFromTemplate(string pid, string templateID, string templateEdition)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                List<string> powerListPossible;              //可能的权限列表
                List<string> powerListFromTmeplate;          //Tmeplate中配置的可用权限
                List<string> powerListForbidden;             //被禁止的权限
                List<string> powerListResult;                //最终的权限
                powerListPossible = new List<string>() { "指定下一办理人", "必须指定下一办理人", "指定下一办理权限" };

                var WFStructure = getWFStructure("", templateID, templateEdition);
                powerListFromTmeplate = WFStructure.wfTemplateInfo.startUserPower.Split(',').ToList();
                powerListForbidden = new List<string>();
                if (powerListFromTmeplate.Exists(delegate(string power) { return power == "指定下一办理人"; }) && powerListFromTmeplate.Exists(delegate(string power) { return power == "必须指定下一办理人"; })
                    || powerListFromTmeplate.Exists(delegate(string power) { return power == "指定下一办理人"; }) && powerListFromTmeplate.Exists(delegate(string power) { return power == "指定下一办理权限"; })
                    || powerListFromTmeplate.Exists(delegate(string power) { return power == "必须指定下一办理人"; }) && powerListFromTmeplate.Exists(delegate(string power) { return power == "指定下一办理权限"; }))
                {
                    powerListForbidden.Add("指定下一办理人"); powerListForbidden.Add("必须指定下一办理人"); powerListForbidden.Add("指定下一办理权限");
                }

                powerListResult = powerListPossible.Intersect(powerListFromTmeplate).ToList();
                powerListResult = powerListResult.Except(powerListForbidden).ToList();
                return powerListResult;
            }
        }

        public List<WF_InstanceState_V> getWFInstanceState(string pid, string instanceId)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                var v = wfDB.WF_InstanceState_V.Where(p => p.instanceID.ToString().Equals(instanceId)).OrderBy(p => p.runState);

                List<WF_InstanceState_V> states = v.ToList();

                return states;
            }
        }

        public List<WF_Instance_V> getWFInstanceByCondition(string pid,string templateId, string department, string startUser, string DatarowId,string status)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                var v = wfDB.WF_Instance_V.AsQueryable();
                if(!string.IsNullOrEmpty(status))
                    v = v.Where(p => p.runState == status);
                if (!string.IsNullOrEmpty(startUser))
                    v = v.Where(p => p.startUser == startUser);
                if (!string.IsNullOrEmpty(DatarowId))
                    v = v.Where(p => p.masterBusinessID.Contains( DatarowId));
                if (!string.IsNullOrEmpty(templateId))
                    v = v.Where(p => p.templateID == templateId);
                if (!string.IsNullOrEmpty(department))
                    v = v.Where(p => p.startDepartment == department);
                
                v = v.OrderByDescending(p => p.startTime);
                return v.ToList();
            }
        }

    }
      
}
